package com.suning.sawp.service.impl.dao;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.dto.customer.CustInfoDetailDto;
import com.suning.sawp.dto.customer.CustInfoListEx;
import com.suning.sawp.dto.customer.CustLabel;
import com.suning.sawp.dto.customer.CustWillComm;
import com.suning.sawp.po.customer.BaseTagGrade;
import com.suning.sawp.po.customer.ClerkCustInfo;
import com.suning.sawp.po.customer.ClerkCustWillProduct;
import com.suning.sawp.po.customer.CustFollowRecord;
import com.suning.sawp.po.customer.CustInfo;
import com.suning.sawp.po.customer.CustLabelRel;
import com.suning.sawp.po.customer.ShareClerkCustInfo;
import com.suning.sawp.service.util.DateUtils;

/**
 * 
 * 会员拓展表相关处理<br>
 * 〈功能详细描述〉 涉及表CLERK_CUST_INFO、CUST_INFO、CUST_LABEL_REL、CUST_FOLLOW_RECORD、CLERK_CUST_WILL_PRODUCT
 * 
 * @author 12061818
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service("clerkCustRelDaoService")
public class ClerkCustRelDaoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ClerkCustRelDaoService.class);

    /**
     * 查询全部
     */
    // private static final String Q_ALL = "0";
    /**
     * 本周
     */
    private static final String Q_THIS_WEEK = "1";
    /**
     * 本月
     */
    private static final String Q_THIS_MONTH = "2";
    /**
     * 更早
     */
    private static final String Q_EARLIER = "3";

    @Resource
    DalClient dalClient;

    /**
     * 
     * 功能描述: 根据手机号查询客户信息<br>
     * 〈功能详细描述〉
     *
     * @param mobile
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public CustInfo queryCustInfoByMobile(String mobile) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("mobile", mobile);
        return dalClient.queryForObject("customer.QUERY_CUST_INFO_BY_MOBILE", paramMap, CustInfo.class);
    }

    /**
     * 
     * 功能描述: 根据custId查询客户信息<br>
     * 〈功能详细描述〉
     *
     * @param custId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public CustInfo queryCustInfoByCustId(Long custId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("custId", custId);
        return dalClient.queryForObject("customer.QUERY_CUST_INFO_BY_CUST_ID", paramMap, CustInfo.class);
    }

    /**
     * 
     * 功能描述: 根据工号和客户ID查询店员客户关系<br>
     * 〈功能详细描述〉
     *
     * @param staffId
     * @param custId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public ClerkCustInfo queryClerkCustInfo(String staffId, Long custId, String storeCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("staffId", staffId);
        paramMap.put("custId", custId);
        paramMap.put("storeCode", storeCode);
        return dalClient.queryForObject("customer.QUERY_CLERK_CUST_INFO", paramMap, ClerkCustInfo.class);
    }

    /**
     * 
     * 功能描述: 根据类型查询客户评级或者客户标签<br>
     * 〈功能详细描述〉
     *
     * @param type
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List<BaseTagGrade> queryTagsByType(String type) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("type", type);
        return dalClient.queryForList("customer.QUERY_TAGS_BY_TYPE", paramMap, BaseTagGrade.class);
    }

    /**
     * 
     * 功能描述: 新增客户信息<br>
     * 〈功能详细描述〉
     *
     * @param custInfo
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public Long addNewCustInfo(CustInfo custInfo) {
        Long custId = (Long) dalClient.persist(custInfo);
        return custId;
    }

    /**
     * 
     * 功能描述: 判断门店是否已经有此客户的外拓关系<br>
     * 〈功能详细描述〉
     *
     * @param custId
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public boolean validCustHasRecordInStore(Long custId, String storeCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("custId", custId);
        paramMap.put("storeCode", storeCode);
        paramMap.put("custSource", ClerkCustInfo.CustSource.OUT_DEV);
        Integer count = dalClient.queryForObject("customer.VALID_STORE_HAS_CUST", paramMap, Integer.class);
        return count > 0 ? true : false;
    }

    /**
     * 
     * 功能描述: 新增店员客户关系<br>
     * 〈功能详细描述〉
     *
     * @param clerkCustInfo
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public Long addNewClerkCustInfo(ClerkCustInfo clerkCustInfo) {
        return (Long) dalClient.persist(clerkCustInfo);
    }

    /**
     * 
     * 功能描述: 根据任务ID查询活动ID<br>
     * 〈功能详细描述〉
     *
     * @param taskId
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public Long queryActIdByTaskId(Long taskId, String storeCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("taskId", taskId);
        paramMap.put("storeCode", storeCode);
        return dalClient.queryForObject("customer.QUERY_ACT_ID_BY_TASK_ID", paramMap, Long.class);
    }

    /**
     * 
     * 功能描述: 新增客户标签关系<br>
     * 〈功能详细描述〉
     *
     * @param custLabelRel
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void addNewCustLabelRels(List<CustLabelRel> custLabelRels) {
        if (CollectionUtils.isNotEmpty(custLabelRels)) {
            List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
            for (CustLabelRel labelRel : custLabelRels) {
                Map<String, Object> paramMap = new HashMap<String, Object>();
                paramMap.put("custRelId", labelRel.getCustRelId());
                paramMap.put("labelId", labelRel.getLabelId());
                paramMap.put("labelName", labelRel.getLabelName());
                paramMap.put("staffId", labelRel.getStaffId());
                mapList.add(paramMap);
            }
            @SuppressWarnings("unchecked")
            Map<String, Object>[] batchValues = (Map<String, Object>[]) mapList.toArray(new Map[mapList.size()]);
            dalClient.batchUpdate("customer.ADD_NEW_CUST_LABEL_REL", batchValues);
        }
    }

    /**
     * 
     * 功能描述: 新增客户意向商品<br>
     * 〈功能详细描述〉
     *
     * @param products
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void addNewCustWillProducts(List<ClerkCustWillProduct> products) {
        if (CollectionUtils.isNotEmpty(products)) {
            List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
            for (ClerkCustWillProduct product : products) {
                Map<String, Object> paramMap = new HashMap<String, Object>();
                paramMap.put("custRelId", product.getCustRelId());
                paramMap.put("staffId", product.getStaffId());
                paramMap.put("commCode", product.getCommCode());
                paramMap.put("commName", product.getCommName());
                paramMap.put("cateCode", product.getCateCode());
                paramMap.put("cateName", product.getCateName());
                paramMap.put("branchCode", product.getBranchCode());
                paramMap.put("branchName", product.getBranchName());
                paramMap.put("remark", product.getRemark());
                mapList.add(paramMap);
            }
            @SuppressWarnings("unchecked")
            Map<String, Object>[] batchValues = (Map<String, Object>[]) mapList.toArray(new Map[mapList.size()]);
            dalClient.batchUpdate("customer.ADD_NEW_CUST_WILL_PRODUCT", batchValues);
        }
    }

    /**
     * 
     * 功能描述: 更新店员客户关系信息<br>
     * 〈功能详细描述〉
     *
     * @param detailDto
     * @param storeCode
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void updateClerkCustInfo(CustInfoDetailDto detailDto, String storeCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("custRelId", detailDto.getCustRelId());
        paramMap.put("remarkName", detailDto.getRemarkName());
        paramMap.put("remark", detailDto.getRemark());
        paramMap.put("custRating", detailDto.getRateId());
        paramMap.put("updateTime", new Date());
        paramMap.put("storeCode", storeCode);
        if(StringUtils.isNotBlank(detailDto.getOrderBuyNum())){
            paramMap.put("orderBuyNum", detailDto.getOrderBuyNum());
        }
        if(StringUtils.isNotBlank(detailDto.getOrderBuyTime())){
            paramMap.put("orderBuyTime", DateUtils.getDateByStr(detailDto.getOrderBuyTime(), DateUtils.DEFAULT_DATE_PATTERN));
        }
        dalClient.execute("customer.UPDATE_CLERK_CUST_INFO", paramMap);
    }

    /**
     * 
     * 功能描述: 删除客户标签列表<br>
     * 〈功能详细描述〉
     *
     * @param custRelId
     * @param staffId
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void delCustLabels(Long custRelId, String staffId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("custRelId", custRelId);
        paramMap.put("staffId", staffId);
        dalClient.execute("customer.DEL_CUST_LABELS", paramMap);
    }

    /**
     * 
     * 功能描述: 删除客户标签列表<br>
     * 〈功能详细描述〉
     *
     * @param custRelId
     * @param staffId
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void delCustWillComms(Long custRelId, String staffId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("custRelId", custRelId);
        paramMap.put("staffId", staffId);
        dalClient.execute("customer.DEL_CUST_WILL_COMMS", paramMap);
    }

    /**
     * 
     * 功能描述: 查询店员客户关系详情<br>
     * 〈功能详细描述〉
     *
     * @param custRelId
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public CustInfoDetailDto queryClerkCustInfoByRelId(Long custRelId, String storeCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("custRelId", custRelId);
        paramMap.put("storeCode", storeCode);
        return dalClient.queryForObject("customer.QUERY_CLERK_CUST_INFO_BY_REL_ID", paramMap, CustInfoDetailDto.class);
    }

    /**
     * 
     * 功能描述: 根据工号和客户关系ID查询客户关系<br>
     * 〈功能详细描述〉
     *
     * @param staffId
     * @param custRelId
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public ClerkCustInfo queryClerkCustInfoByStaffIdAndRelId(String staffId, Long custRelId, String storeCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("staffId", staffId);
        paramMap.put("custRelId", custRelId);
        paramMap.put("storeCode", storeCode);
        return dalClient.queryForObject("customer.QUERY_CLERK_CUST_INFO_BY_STAFF_ID_REL_ID", paramMap,
                ClerkCustInfo.class);
    }

    /**
     * 
     * 功能描述: 查询客户标签列表<br>
     * 〈功能详细描述〉
     *
     * @param custRelId
     * @param staffId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List<CustLabel> queryCustLabels(Long custRelId, String staffId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("custRelId", custRelId);
        paramMap.put("staffId", staffId);
        return dalClient.queryForList("customer.QUERY_CUST_LABELS", paramMap, CustLabel.class);
    }

    /**
     * 
     * 功能描述: 查询客户意向商品列表<br>
     * 〈功能详细描述〉
     *
     * @param custRelId
     * @param staffId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List<CustWillComm> queryCustWillComms(Long custRelId, String staffId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("custRelId", custRelId);
        paramMap.put("staffId", staffId);
        return dalClient.queryForList("customer.QUERY_CUST_WILL_COMMS", paramMap, CustWillComm.class);
    }

    /**
     * 
     * 功能描述: 查询店员拓展所有的客户列表<br>
     * 〈功能详细描述〉
     *
     * @param staffId
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List<CustInfoListEx> getMyCustInfoList(String staffId, String storeCode, String promoteDate) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("staffId", staffId);
        paramMap.put("storeCode", storeCode);
        Date startTime = null;
        Date endTime = null;
        if (Q_THIS_WEEK.equals(promoteDate)) {
            startTime = DateUtils.getTimesWeekmorning();
            endTime = DateUtils.getTimesWeeknight();
        } else if (Q_THIS_MONTH.equals(promoteDate)) {
            startTime = DateUtils.getFirstDayZeroPointOfMonth(0);
            endTime = DateUtils.getFirstDayZeroPointOfMonth(1);
        } else if (Q_EARLIER.equals(promoteDate)) {
            endTime = DateUtils.getFirstDayZeroPointOfMonth(0);
        }
        paramMap.put("startTime", startTime);
        paramMap.put("endTime", endTime);
        return dalClient.queryForList("customer.QUERY_MY_ALL_CUSTS", paramMap, CustInfoListEx.class);
    }

    /**
     * 
     * 功能描述: 取店员待跟进的记录列表<br>
     * 〈功能详细描述〉
     *
     * @param staffId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List<Long> getWaitFollowRecords(String staffId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("staffId", staffId);
        paramMap.put("nextFollowTime", DateUtils.getDateStrByFormat(new Date(), DateUtils.YYYYMMDD_PATTERN));
        paramMap.put("followType", CustFollowRecord.FollowType.THOUSAND_SOUND);
        return dalClient.queryForList("customer.GET_WAIT_FOLLOW_RECORDS", paramMap, Long.class);
    }

    /**
     * 
     * 功能描述: 查询店员某日到店客户列表<br>
     * 〈功能详细描述〉
     *
     * @param staffId
     * @param storeCode
     * @param queryDate
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List<CustInfoListEx> getArriveStoreCustList(String staffId, String storeCode, Date beginTime, Date endTime) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("staffId", staffId);
        paramMap.put("storeCode", storeCode);
        paramMap.put("beginTime", beginTime);
        paramMap.put("endTime", endTime);
        paramMap.put("custSource", ClerkCustInfo.CustSource.ARRIVE_STORE);
        return dalClient.queryForList("customer.GET_ARRIVE_STORE_CUSTS", paramMap, CustInfoListEx.class);
    }

    /**
     * 功能描述: 查询店员某日到店客户列表 分页查询
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param staffId
     * @param storeCode
     * @param index
     * @param page
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List<CustInfoListEx> getArriveStoreCustPageList(String staffId, String storeCode, int index, int page) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("staffId", staffId);
        paramMap.put("storeCode", storeCode);
        paramMap.put("index", index);
        paramMap.put("page", page);
        paramMap.put("custSource", ClerkCustInfo.CustSource.ARRIVE_STORE);
        return dalClient.queryForList("customer.GET_ARRIVE_STORE_CUSTS_NEW", paramMap, CustInfoListEx.class);
    }

    
    /**
     * 
     * 功能描述: 查询熟人渠道客户列表<br>
     * 〈功能详细描述〉
     *
     * @param staffId
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List<CustInfoListEx> getAcqRecommendCusts(String staffId, String storeCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("staffId", staffId);
        paramMap.put("storeCode", storeCode);
        paramMap.put("custSource", ClerkCustInfo.CustSource.ACQ_RECOMMEND);
        return dalClient.queryForList("customer.GET_ACQ_RECOMMEND_CUSTS", paramMap, CustInfoListEx.class);
    }

    /**
     * 
     * 功能描述: 新增客户跟进记录<br>
     * 〈功能详细描述〉
     *
     * @param followRecord
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void addNewCustFollowRecord(CustFollowRecord followRecord) {
        dalClient.persist(followRecord);
    }

    /**
     * 
     * 功能描述: 查询所有客户跟进记录<br>
     * 〈功能详细描述〉
     *
     * @param staffId
     * @param custRelId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List<CustFollowRecord> queryCustFollowRecords(String staffId, Long custRelId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("staffId", staffId);
        paramMap.put("custRelId", custRelId);
        return dalClient.queryForList("customer.QUERY_CUST_FOLLOW_RECORDS", paramMap, CustFollowRecord.class);
    }

    /**
     * 
     * 功能描述: 设置或取消设置重点客户<br>
     * 〈功能详细描述〉
     *
     * @param custRelId
     * @param storeCode
     * @param opType
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void setKeyCust(Long custRelId, String storeCode, Integer opType) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("custRelId", custRelId);
        paramMap.put("storeCode", storeCode);
        paramMap.put("isKeyCust", opType);
        dalClient.execute("customer.SET_KEY_CUST", paramMap);
    }

    /**
     * 
     * 功能描述: 更新客户信息中的手机号<br>
     * 〈功能详细描述〉
     *
     * @param custId
     * @param mobile
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void updateCustInfoMobile(Long custId, String mobile) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("custId", custId);
        paramMap.put("mobile", mobile);
        dalClient.execute("customer.UPDATE_CUST_INFO_MOBILE", paramMap);
    }

    /**
     * 
     * 功能描述: 更新客户信息中的手机号<br>
     * 〈功能详细描述〉
     *
     * @param custId
     * @param mobile
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void updateCustInfoCustNo(Long custId, String custNo) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("custId", custId);
        paramMap.put("custNo", custNo);
        dalClient.execute("customer.UPDATE_CUST_INFO_CUST_NO", paramMap);
    }

    /**
     * 
     * 功能描述: 根据客户ID查询涉及的客户关系列表<br>
     * 〈功能详细描述〉
     *
     * @param custId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List<ClerkCustInfo> queryClerkCustInfosByCustId(Long custId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("custId", custId);
        return dalClient.queryForList("customer.QUERY_CLERK_CUST_INFO_BY_CUST_ID", paramMap, ClerkCustInfo.class);
    }

    /**
     * 
     * 功能描述: 根据客户关系ID， 店员工号删除跟进记录<br>
     * 〈功能详细描述〉
     *
     * @param custRelId
     * @param staffId
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void delCustFollowRecords(Long custRelId, String staffId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("custRelId", custRelId);
        paramMap.put("staffId", staffId);
        dalClient.execute("customer.DEL_CUST_FOLLOE_RECORDS", paramMap);
    }

    /**
     * 
     * 功能描述: 根据客户ID删除客户信息<br>
     * 〈功能详细描述〉
     *
     * @param custId
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void delCustInfo(Long custId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("custId", custId);
        dalClient.execute("customer.DEL_CUST_INFO_BY_CUST_ID", paramMap);
    }

    /**
     * 
     * 功能描述: 查询门店下的所属共享客户关系<br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List<CustInfoListEx> queryShareCustList(String storeCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        return dalClient.queryForList("customer.QUERY_BELONG_ALL_SHARE_CUSTS", paramMap, CustInfoListEx.class);
    }

    /**
     * 
     * 功能描述: 根据客户关系ID查询共享关系<br>
     * 〈功能详细描述〉
     *
     * @param custRelId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public ShareClerkCustInfo queryShareClerkCustInfo(Long custRelId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("custRelId", custRelId);
        return dalClient.queryForObject("customer.QUERY_SHARE_CLERK_CUST_INFO", paramMap, ShareClerkCustInfo.class);
    }

    @Transactional
    public void dealAssginShareClerkCustInfo(String assignStaffId, Long custRelId,
            ShareClerkCustInfo shareClerkCustInfo) throws IllegalAccessException, InvocationTargetException {
        // 1、删除CLERK_CUST_INFO中原有的关系
        LOGGER.info("ClerkCustRelService.assignShareCust deleteClerkCustInfo . custRelId:{}", custRelId);
        deleteClerkCustInfo(custRelId, shareClerkCustInfo.getStoreCode());
        // 2、在CLERK_CUST_INFO中插入一条新的记录
        ClerkCustInfo clerkCustInfo = new ClerkCustInfo();
        BeanUtils.copyProperties(clerkCustInfo, shareClerkCustInfo);
        // 客户关系ID置空
        clerkCustInfo.setCustRelId(null);
        // 客户关系重新置为新的分配工号
        clerkCustInfo.setStaffId(assignStaffId);
        // 生成一条新的客户关系记录
        LOGGER.info("ClerkCustRelService.assignShareCust insertNewClerkCustInfo.");
        Long newCustRelId = insertNewClerkCustInfo(clerkCustInfo);
        // 4、更改以前相关联的客户关系附属信息
        // 涉及CUST_LABEL_REL（客户和标签关联表）、CUST_FOLLOW_RECORD（客户跟进记录表）、CLERK_CUST_WILL_PRODUCT（店员-客户-意向商品表）
        LOGGER.info("ClerkCustRelService.assignShareCust update clerkCust relation tables.");
        updateCustLabelRel(custRelId, shareClerkCustInfo.getStaffId(), newCustRelId, assignStaffId);
        updateCustFollowRecord(custRelId, shareClerkCustInfo.getStaffId(), newCustRelId, assignStaffId);
        updateClerkCustWillProduct(custRelId, shareClerkCustInfo.getStaffId(), newCustRelId, assignStaffId);
        // 5、删除共享客户关系表中对应记录
        LOGGER.info("ClerkCustRelService.assignShareCust delShareClerkCustInfo. custRelId:{}", custRelId);
        delShareClerkCustInfo(custRelId, shareClerkCustInfo.getStoreCode());
    }

    /**
     * 
     * 功能描述: 删除店员客户关系<br>
     * 〈功能详细描述〉
     *
     * @param custRelId
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void deleteClerkCustInfo(Long custRelId, String storeCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("custRelId", custRelId);
        paramMap.put("storeCode", storeCode);
        dalClient.execute("customer.DEL_CLERK_CUST_INFO", paramMap);
    }

    /**
     * 
     * 功能描述: 增加一条新的店员客户关系记录<br>
     * 〈功能详细描述〉 仅供重新分配共享客户关系场景使用
     * 
     * @param clerkCustInfo
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public Long insertNewClerkCustInfo(ClerkCustInfo clerkCustInfo) {
        return (Long) dalClient.persist(clerkCustInfo);
    }

    /**
     * 
     * 功能描述: 更新客户标签关联关系<br>
     * 〈功能详细描述〉 仅供重新分配共享客户关系场景使用
     * 
     * @param oldCustRelId
     * @param oldStaffId
     * @param newCustRelId
     * @param newStaffId
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void updateCustLabelRel(Long oldCustRelId, String oldStaffId, Long newCustRelId, String newStaffId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("oldCustRelId", oldCustRelId);
        paramMap.put("newCustRelId", newCustRelId);
        paramMap.put("oldStaffId", oldStaffId);
        paramMap.put("newStaffId", newStaffId);
        dalClient.execute("customer.UPDATE_CUST_LABEL_REL", paramMap);
    }

    /**
     * 
     * 功能描述: 更新客户跟进记录表<br>
     * 〈功能详细描述〉 仅供重新分配共享客户关系场景使用
     * 
     * @param oldCustRelId
     * @param oldStaffId
     * @param newCustRelId
     * @param newStaffId
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void updateCustFollowRecord(Long oldCustRelId, String oldStaffId, Long newCustRelId, String newStaffId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("oldCustRelId", oldCustRelId);
        paramMap.put("newCustRelId", newCustRelId);
        paramMap.put("oldStaffId", oldStaffId);
        paramMap.put("newStaffId", newStaffId);
        dalClient.execute("customer.UPDATE_CUST_FOLLOW_RECORD", paramMap);
    }

    /**
     * 
     * 功能描述: 更新店员-客户-意向商品表<br>
     * 〈功能详细描述〉 仅供重新分配共享客户关系场景使用
     * 
     * @param oldCustRelId
     * @param oldStaffId
     * @param newCustRelId
     * @param newStaffId
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void updateClerkCustWillProduct(Long oldCustRelId, String oldStaffId, Long newCustRelId, String newStaffId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("oldCustRelId", oldCustRelId);
        paramMap.put("newCustRelId", newCustRelId);
        paramMap.put("oldStaffId", oldStaffId);
        paramMap.put("newStaffId", newStaffId);

        dalClient.execute("customer.UPDATE_CLERK_CUST_WILL_PRODUCT", paramMap);
    }

    /**
     * 
     * 功能描述: 删除共享客户关系表中的记录<br>
     * 〈功能详细描述〉 仅供重新分配共享客户关系场景使用
     * 
     * @param custRelId
     * @param storeCode
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void delShareClerkCustInfo(Long custRelId, String storeCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("custRelId", custRelId);
        paramMap.put("storeCode", storeCode);
        dalClient.execute("customer.DEL_SHARE_CLERK_CUST_INFO", paramMap);
    }

    /**
     * 
     * 功能描述: 查询店员归属的店员客户关系总记录数<br>
     * 〈功能详细描述〉
     *
     * @param staffId
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public Integer countClerkCustInfoByStaffId(String staffId, String storeCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("staffId", staffId);
        paramMap.put("storeCode", storeCode);
        return dalClient.queryForObject("customer.COUNT_CLERK_CUST_INFO", paramMap, Integer.class);
    }

    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param staffId
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List<ClerkCustInfo> queryBelongClerkCustInfos(String staffId, String storeCode, int startIndex, int limit) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("staffId", staffId);
        paramMap.put("storeCode", storeCode);
        paramMap.put("startIndex", startIndex);
        paramMap.put("limit", limit);
        return dalClient.queryForList("customer.QUERY_BELONG_CLERK_CUST_INFO", paramMap, ClerkCustInfo.class);
    }

    /**
     * 
     * 功能描述: 批量插入共享客户关系表<br>
     * 〈功能详细描述〉
     *
     * @param shareList
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @Transactional
    public void batchAddShareClerkCustInfo(List<Map<String, Object>> shareList) {
        @SuppressWarnings("unchecked")
        Map<String, Object>[] batchValues = (Map<String, Object>[]) shareList.toArray(new Map[shareList.size()]);
        // 执行SQL操作
        dalClient.batchUpdate("customer.ADD_NEW_SHARE_CLERK_CUST_INFO", batchValues);
    }

    /**
     * 
     * 功能描述: 校验店员客户关系是否存在<br>
     * 〈功能详细描述〉
     *
     * @param staffId
     * @param custId
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public boolean checkClerkCustInfoExists(String staffId, Long custId, String storeCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("staffId", staffId);
        paramMap.put("custId", custId);
        paramMap.put("storeCode", storeCode);
        Integer count = dalClient.queryForObject("customer.VALID_CLERK_CUST_INFO_EXISTS", paramMap, Integer.class);
        if (null != count && count > 0) {
            return true;
        } else {
            return false;
        }
    }

    public Integer queryNoMemberNumByStaffId(String staffId, String storeCode) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("staffId", staffId);
        map.put("storeCode", storeCode);
        return dalClient.queryForObject("customer.QUERY_NO_MEMBER_NUM", map, Integer.class);
    }

    /**
     * 
     * 功能描述: 根据客户关系ID和门店编码查询客户关系所属工号<br>
     * 〈功能详细描述〉
     *
     * @param custRelId
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public String queryClerkCustInfoBelongStaffId(Long custRelId, String storeCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("custRelId", custRelId);
        paramMap.put("storeCode", storeCode);
        return dalClient.queryForObject("customer.QUERY_BELONG_STAFF_ID", paramMap, String.class);
    }

    public Integer queryIndexOrderNum(String staffId, String storeCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("staffId", staffId);
        paramMap.put("storeCode", storeCode);
        Integer count = dalClient.queryForObject("customer.QUERY_INDEX_ORDER_NUM", paramMap, Integer.class);
        return count;
    }
    
    public List<CustInfoListEx> getMyOrderInfoList(String staffId, String storeCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("staffId", staffId);
        paramMap.put("storeCode", storeCode);
        return dalClient.queryForList("customer.QUERY_MY_ORDER_CUSTS", paramMap, CustInfoListEx.class);
    }
    
    /**
     * 
     * 功能描述: 根据会员编码和门店编码查询对应的绑定工号<br>
     * 〈功能详细描述〉
     *
     * @param custNo
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public String getStaffIdByCustNo(String custNo, String storeCode) {
        // 根据会员编码查询custId
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("custNo", custNo);
        String custId = dalClient.queryForObject("customer.QUERY_CUST_ID_BY_CUST_NO", paramMap, String.class);
        // 根据custId和门店编码查询对应的工号
        paramMap.put("custId", custId);
        paramMap.put("storeCode", storeCode);
        String staffId = dalClient.queryForObject("customer.QUERY_STAFF_ID_BY_STORE_CD_CUST_ID", paramMap, String.class);
        return staffId;
    }
}
